package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 保存菜品和口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlaover(DishDTO dishDTO) {
        //1. 构建pojo
        Dish dish = new Dish();
        //2. 拷贝数据
        BeanUtils.copyProperties(dishDTO, dish);

        //3. 插入菜品
        dishMapper.insert(dish);

        //4. 获取菜品的id
        Long dishId = dish.getId();

        //5. 判断口味数据是否为空 不为空则添加口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(null != flavors && flavors.size() >0){
            //5.1 给每个口味数据添加菜品的id
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //5.2 批量添加口味数据
            flavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        if(null != dishPageQueryDTO.getCategoryId()){
            dish.setCategoryId(dishPageQueryDTO.getCategoryId().longValue());
        }

        Page<DishVO> page = dishMapper.pageQuery(dish);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //1. 如果删除的菜品中有起售的商品那么就不允许删除
        ids.forEach(id ->{
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //2. 如果套餐中关联了我们要删除的菜品也不允许删除
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(null != setmealIds && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3. 删除菜品的时候要同时删除口味数据
        ids.forEach(id ->{
            dishMapper.delete(id);

            flavorMapper.deleteByDishId(id);
        });
    }

    /**
     * 根据id查询菜品和菜品的口味数据
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);

        List<DishFlavor> flavors=  flavorMapper.getFlaversByDishId(id);

        DishVO vo = new DishVO();
        BeanUtils.copyProperties(dish, vo);
        vo.setFlavors(flavors);

        return vo;
    }

    /**
     * 菜品和口味的更新
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        //1. 创建pojo
        Dish dish = new Dish();

        //2. 拷贝dto的数据到pojo
        BeanUtils.copyProperties(dishDTO, dish);

        //3. 更新菜品数据
        dishMapper.update(dish);

        //4. 删除菜品的口味数据
        flavorMapper.deleteByDishId(dishDTO.getId());

        //5. 新增菜品的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(null != flavors && flavors.size() > 0){
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });

            flavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<DishVO> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        Page<DishVO> page = dishMapper.pageQuery(dish);


        return page.getResult();
    }

    /**
     * 阅读代码总结:
     *  1. 启动前端和后端程序
     *  2. 在前端中找到你要看的功能然后通过F12看他发送的请求
     *  3. 看请求
     *      1. 看请求的地址
     *      2. 看请求的方式
     *      3. 看请求的参数
     *  4. 通过请求的地址找到一个关键字然后在idea中去搜索 找到类和方法
     *  5. 在方法的第一行打一个断点 然后一行一行的去执行代码 查看每一行代码具体的含义
     */

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.pageQuery(dish);

       // List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishList) {
           /* DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);*/

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlaversByDishId(d.getId());
            d.setFlavors(flavors);
            /*dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);*/
        }

        return dishList;
    }
}
